import React, { FC, useState } from "react";

/** dataList 的核心。 */

function request<T = void>(url: string, option: Object): Promise<T> {
  return fetch(url, option)
    .then((res) => res.json())
    .then((data) => data);
}

const fetchData = (url: string, params?: Object) =>
  request<number[]>("/addd/", {});

function DataList<T>(
  api: (url: string, option: string) => Promise<T[]>,
  render: (item: T) => void
) {
  const [data, setData] = useState<T[]>();
  return (
    <div>
      {data?.map((item) => {
        render?.(item);
      })}
    </div>
  );
}
const foo = <T,>(x: T) => x;
const foo1 = <T extends unknown>(x: T) => x;

DataList(fetchData, (item) => {
  console.log(item);
});

export default DataList;
